<?php

require_once 'db.inc';
require_once 'data.inc';


class GalleryAlbum
{
	/** @param $id  string CODE or nimeric ID */
	function __construct($id)
	{
		$this->id = self::toID($id);
		
		static $q = null;
		createStaticQuery($q,"
			SELECT name, code
			FROM cf_gallery_albums
			WHERE id=:id
		");
		$q->setParam('id',$this->id);
		$q->execute();
		if (!$q->fetch()) {
			throw new Exception("No Gallery album with id [{$this->id}]");
		}
		
		$this->name = $q->at(0);
		$this->code = $q->at(1);
		
		$q->close();
	}
	
	public function id()
	{
		return $this->id;
	}
	
	/** @return  string CODE or numeric ID */
	public function code()
	{
		return strlen($this->code) ? $this->code : $this->id;
	}
	
	public function name() 
	{
		return $this->name;
	}
	
	public function descr() 
	{
		if (!is_null($this->descr)) {
			return $this->descr;
		}
		
		static $q = null;
		createStaticQuery($q,"
			SELECT descr
			FROM cf_gallery_albums
			WHERE id=:id
		");
		$q->setParam('id',$this->id);
		$q->execute();
		if (!$q->fetch()) {
			throw new Exception("No gallery album with id [{$this->id}]");
		}
		$this->descr = $q->at(0);
		$q->close();
		
		return $this->descr;
	}
	
			//image file URL (false if none)
	public function image()
	{
		if (!is_null($this->image)) {
			return $this->image;
		}
		
		$image = new Files('gallery_albums');
		$image->open($this->id, 'image');
		if ($image->fetch()) {
			$this->image = $image->current();
		}
		else {
			$this->image = false;
		}
		
		return $this->image;
	}
	
	/**
		@param $id  string CODE or numeric ID
		@return numeric ID
	*/
	static public function toID($id)
	{
		if (is_numeric($id)) {
			return (int)$id;
		}
		
		static $q = null;
		createStaticQuery($q,"
			SELECT id FROM cf_gallery_albums WHERE code=:code
		");
		$q->setParam('code',trim($id));
		$q->execute();
		if (!$q->fetch()) {
			throw new Exception("Invalid gallery album code: [$id]");
		}
		$id = (int)$q->at(0);
		$q->close();
		
		return $id;
	}
	
	private $id = null;
	private $code = null;
	private $name = '';
	private $descr = null;
	private $image = null;
};



class GalleryAlbums
	extends ArrayOfItems
{
	function __construct()
	{
		static $q = null;
		createStaticQuery($q,"
			SELECT id
			FROM cf_gallery_albums
			ORDER BY sort_order ASC
		");
		$q->execute();
		while ($q->fetch()) {
			$this->add(new GalleryAlbum($q->at(0)));
		}
		$q->close();
	}
	
	public function get($id) 
	{
		$id = GalleryAlbum::toID($id);
		foreach ($this->items as $album) 
		{
			if ($album->id() == $id) {
				return $album;
			}
		}
		return false;
	}
	
	public function exist($id) 
	{
		return $this->get($id) !== false;
	}
	
	
	protected function add(GalleryAlbum $item) 
	{
		parent::add($item);
	}
};




class GalleryImage
{
	/** @param $id  string CODE or nimeric ID */
	function __construct($id)
	{
		$this->id = self::toID($id);
		
		static $q = null;
		createStaticQuery($q,"
			SELECT name, album_id, code
			FROM cf_gallery
			WHERE id=:id
		");
		$q->setParam('id',$this->id);
		$q->execute();
		if (!$q->fetch()) {
			throw new Exception("No Gallery image with id [{$this->id}]");
		}
		
		$this->name = $q->at(0);
		$this->albumId = $q->at(1);
		$this->code = $q->at(2);
		
		$q->close();
	}
	
	public function id() 
	{
		return $this->id;
	}
	
		/** @return  string CODE or numeric ID */
	public function code()
	{
		return strlen($this->code) ? $this->code : $this->id;
	}
	
	public function albumId() 
	{
		return $this->albumId;
	}
	
	public function name() 
	{
		return $this->name;
	}
	
	public function descr() 
	{
		if (!is_null($this->descr)) {
			return $this->descr;
		}
		
		static $q = null;
		createStaticQuery($q,"
			SELECT descr
			FROM cf_gallery
			WHERE id=:id
		");
		$q->setParam('id',$this->id);
		$q->execute();
		if (!$q->fetch()) {
			throw new Exception("No gallery image with id [{$this->id}]");
		}
		$this->descr = $q->at(0);
		$q->close();
		
		return $this->descr;
	}
	
		//image file URL (false if none)
	public function image()
	{
		if (!is_null($this->image)) {
			return $this->image;
		}
		
		$image = new Files('gallery');
		$image->open($this->id, 'image');
		if ($image->fetch()) {
			$this->image = $image->current();
		}
		else {
			$this->image = false;
		}
		
		return $this->image;
	}
	
	
	/**
		@param $id  string CODE or numeric ID
		@return numeric ID
	*/
	static public function toID($id)
	{
		if (is_numeric($id)) {
			return (int)$id;
		}
		
		static $q = null;
		createStaticQuery($q,"
			SELECT id FROM cf_gallery WHERE code=:code
		");
		$q->setParam('code',trim($id));
		$q->execute();
		if (!$q->fetch()) {
			throw new Exception("Invalid gallery image code: [$id]");
		}
		$id = (int)$q->at(0);
		$q->close();
		
		return $id;
	}
	
	private $id = null;
	private $code = null;
	private $name = '';
	private $descr = null;
	private $image = null;
	private $albumId = null;
};



class RandomGalleryImage 
	extends GalleryImage
{
	/** @param $albumId  string CODE or nimeric ID */
	function __construct($albumId=null)
	{
		$albumId = $albumId ? GalleryAlbum::toID($albumId) : null;
		
		$sql = "SELECT id FROM cf_gallery".($albumId ? " WHERE album_id=$albumId" : '');
		$id = query2var(
				limitSelect(
					$sql,
					1,
					rand(0, DB::countRecords($sql)-1)
				)
			 );
		parent::__construct($id);
	}
};


class GalleryImages
	extends ArrayOfItems
{
	/** @param $albumId  string CODE or nimeric ID */
	function __construct($albumId, $limit=null, $skip=null)
	{
		$albumId = GalleryAlbum::toID($albumId);
		
		static $q = null;
		$q = createQuery(
			limitSelect("
				SELECT id
				FROM cf_gallery
				WHERE album_id=:albumId
				ORDER BY sort_order ASC
				", 
				$limit,
				$skip
			)
		);
		$q->setParam('albumId',$albumId);
		$q->execute();
		while ($q->fetch()) 
		{
			$this->add(new GalleryImage($q->at(0)));
		}
		$q->close();
	}
	
	public function get($id) 
	{
		foreach ($this->items as $image) 
		{
			if ($image->id() == $id) {
				return $image;
			}
		}
		return false;
	}
	
	public function exist($id) 
	{
		return $this->get($id) !== false;
	}
	
	
	static public function countAll($albumId)
	{
		$albumId = GalleryAlbum::toID($albumId);
		return query2var("SELECT COUNT(*) FROM cf_gallery WHERE album_id=$albumId");
	}
	
	
	protected function add(GalleryImage $item) 
	{
		parent::add($item);
	}
};

?>